package org.leopard.rpc;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Set;

/**
 * @author IBM CHEN ZHI KAN
 * @create 2018/8/13
 */
public abstract class Consumer<T> {
  private String url;

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  /**
   * 调用远程服务
   * @return
   */
  public    T lookupService(){
    Set<String> urls=ServiceCenter.getInstance("localhost").addres.get(this.getUrl());
    Object[] urlArray=urls.toArray();
    if(urlArray.length>0){
      try {
        return (T)Naming.lookup(String.valueOf(urlArray[0]));
      } catch (NotBoundException e) {
        e.printStackTrace();
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (RemoteException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}
